package com.happyscan138.makefun;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.Fragment.Persionalvideo;
import com.happyscan138.makefun.Fragment.persional_questuion;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.adapter.MyFragmentPagerAdapter;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/3.
 */

public class more_myself extends AppCompatActivity implements View.OnClickListener{
    private ImageButton back;
    private Button buttonCreditor;
    private TextView textViewQuestion;
    private TextView textViewvideo;
    private ImageView imagetab;//下划线
    private ArrayList<Fragment> fragmentsList;
    private ViewPager mPagerMyDiscuss;
    private int position_one;//初始位移
    private int pWidth;//宽度
    private int offset ;// 图片偏移量
    public final static int num = 2;
    private int currIndex = 0;
    private Fragment question;
    private Fragment video;
    private TextView textNickName,textQianming;
    private RoundImageView head;
    //声明一个SharedPreferences对象和一个Editor对象
    private SharedPreferences preferences;
    private String accessy_ley;
    private Intent intent;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    private String PersionalUserID;
    public String UserID;
    private RequestParams paramsInfo;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_myself);
        intent=getIntent();
        UserID=intent.getStringExtra("infoUserID");
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        accessy_ley=preferences.getString("UserId","");
        PersionalUserID=preferences.getString("persionalID","");
        initView();
        InitViewPager();
        InitWidth();


        log.e("infoUserIDReceive",UserID+"");
        paramsInfo=new RequestParams();
        paramsInfo.put("access_key",accessy_ley);
        paramsInfo.put("user_id",UserID);
        client.post(Urls.Info, paramsInfo,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr = response.toString();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONObject data=jsonObject.getJSONObject("data");
                    String nick_name=data.getString("nick_name");
                    String sign=data.getString("sign");
                    String imagePath= Urls.base+data.getString("img_path");
                    if(data!=null) {
                        textNickName.setText(nick_name);
                        textQianming.setText(sign);
                        Picasso.with(more_myself.this).load(imagePath).into(head);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                log.e("onSuccess",response.toString());
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("onFailure",responseString);
            }
        });
        TranslateAnimation animation = new TranslateAnimation(0, offset, 0, 0);
        animation.setFillAfter(true);
        animation.setDuration(300);
        imagetab.startAnimation(animation);
    }
    public void initView(){
        back=(ImageButton)findViewById(R.id.back) ;
        back.setOnClickListener(this);
        textNickName=(TextView) findViewById(R.id.moreMyself_nickName);
        textQianming=(TextView)findViewById(R.id.moreMyself_qianming);
        head=(RoundImageView) findViewById(R.id.persioanl_head);
        buttonCreditor=(Button)findViewById(R.id.buttonCreditor) ;
        if(PersionalUserID.equals(UserID)){
            log.e("PersionalUserID",PersionalUserID);
            buttonCreditor.setVisibility(View.VISIBLE);
        }
        buttonCreditor.setOnClickListener(this);

        imagetab=(ImageView)findViewById(R.id.persioanl_tab);
        textViewQuestion=(TextView)findViewById(R.id.persional_text_question);
        textViewvideo=(TextView)findViewById(R.id.persional_text_video);

        textViewQuestion.setOnClickListener(this);
        textViewvideo.setOnClickListener(this);
    }
    private void InitViewPager( ) {
        Bundle bundle=new Bundle();
        bundle.putString("user_id",UserID);
        mPagerMyDiscuss = (ViewPager)findViewById(R.id.persional_viewpager);
        question = new persional_questuion();
        log.e("bundle",UserID);
        question.setArguments(bundle);

        video = new Persionalvideo();
        video.setArguments(bundle);
        fragmentsList = new ArrayList<Fragment>();
        fragmentsList.add(question);
        fragmentsList.add(video);

        mPagerMyDiscuss.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPagerMyDiscuss.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Animation animation = null;
                switch (position) {
                    case 0:
                        if (currIndex == 1) {
                            animation = new TranslateAnimation(position_one, offset, 0, 0);
                        }

                        break;
                    case 1:
                        if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, position_one, 0, 0);
                        }

                        break;
                }
                currIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(300);
                imagetab.startAnimation(animation);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
        });
        mPagerMyDiscuss.setCurrentItem(0);
    }
    private void InitWidth( ) {
        BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.mipmap.up_tab);
        Bitmap bmp = bd.getBitmap();
        pWidth = bmp.getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / num -pWidth)) / 2;//偏移量
        int avg = (int) (screenW / num);
        position_one = avg+offset;//起始位置

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.persional_text_question:
                mPagerMyDiscuss.setCurrentItem(0);
                break;
            case R.id.persional_text_video:
                mPagerMyDiscuss.setCurrentItem(1);
                break;
            case R.id.buttonCreditor:
                startActivity(new Intent(more_myself.this,Creditor.class));
                break;
            case R.id.back:
                finish();
                break;
        }

    }

}
